package de.devisnik.eidle.ide;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.internal.WorkbenchPage;
import org.eclipse.ui.progress.UIJob;

import de.devisnik.eidle.app.EidlePerspective;
import de.devisnik.eidle.util.ScreenUtils;

public class WorbenchActivityTracker {

	private class ScreenSaverStarterJob extends UIJob {

		private final IWorkbench itsWorkbench;

		private boolean itsIsRunning;

		private final IPerspectiveDescriptor itsPerspectiveDescpriptor;

		private IWorkbenchPage itsEidlePage;

		public ScreenSaverStarterJob(final IWorkbench workbench) {
			super("eidle runner");
			this.itsWorkbench = workbench;
			itsPerspectiveDescpriptor = itsWorkbench.getPerspectiveRegistry()
					.findPerspectiveWithId(EidlePerspective.ID);
		}

		private void startEidle() throws PartInitException {
			try {
				takeScreenShot();

				itsEidlePage = itsWorkbench.showPerspective(
						EidlePerspective.ID, itsWorkbench
								.getActiveWorkbenchWindow());
			} catch (final WorkbenchException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		public void closeScreenSaverView() {
			if (itsEidlePage == null) {
				return;
			}
			itsEidlePage.closePerspective(itsPerspectiveDescpriptor, false,
					false);
		}

		// @Override
		public IStatus runInUIThread(final IProgressMonitor monitor) {
			// System.out.println("running at " + new Date());
			try {
				startEidle();
				itsIsRunning = true;

			} catch (final PartInitException e) {
				e.printStackTrace();
				return new Status(IStatus.ERROR, "", 0,
						"unable to open screensaver view", e);
			}
			return Status.OK_STATUS;
		}

		void stop() {
			if (itsIsRunning) {
				closeScreenSaverView();
				itsIsRunning = false;
			}
			cancel();
		}
	}

	private ScreenSaverStarterJob itsStarterJob;

	private final IWorkbench workbench;

	private final IDelayProvider delayProvider;

	public WorbenchActivityTracker(final IWorkbench workbench,
			final IDelayProvider delayProvider) {
		this.workbench = workbench;
		this.delayProvider = delayProvider;
		itsStarterJob = new ScreenSaverStarterJob(workbench);
	}

	public void notifyActivity() {

		itsStarterJob.stop();
		itsStarterJob.schedule(delayProvider.getDelay());
	}

	private void takeScreenShot() {
		final WorkbenchPage page = (WorkbenchPage) workbench
				.getActiveWorkbenchWindow().getActivePage();
		// final Rectangle bounds =
		// page.getPerspectivePresentation().getLayout()
		// .getBounds();
		// System.out.println("perspective-Bounds: " + bounds.x + "," + bounds.y
		// + "," + bounds.width + "," + bounds.height);
		final Control control = page.getPerspectivePresentation().getLayout()
				.getParent().getParent();
		final Image snapShot = ScreenUtils.takeSnapShot(control);

		// Image snapShot =
		// ScreenUtils.takeSnapShot(workbench.getActiveWorkbenchWindow().getShell());
		ScreenUtils.setScreenImage(snapShot);
	}

	public void dispose() {
		itsStarterJob.stop();
		itsStarterJob = null;
	}
}
